// 云函数入口文件
const cloud = require('wx-server-sdk')

cloud.init({
  traceUser: true,
  env:'aaa111-9gh9fp5ieac4de5d'
})

const db = cloud.database()

// 云函数入口函数
exports.main = async (event, context) => {
    const wxContext = cloud.getWXContext()
    console.log(event)
    console.log(wxContext.OPENID)
    duringDays = (event.tripEndTime - event.tripBeginTime) / (1 * 24 * 60 * 60 * 1000) + 1;
    var days = []
    for (var i =0; i<duringDays; i++) {
        await db.collection('tripDays').add({
            data: {}
        }).then(res => {
            days.push(res._id)
        })
    }
    console.log(days)
    try {
        return await db.collection('trip').add({
            data: {
                image: event.image,
                isPub: event.isPub,
                title: event.title,
                tripBeginTime: event.tripBeginTime,
                tripEndTime: event.tripEndTime,
                days: days,
                _openid: wxContext.OPENID
            }
        })
    } catch(e) {
        console.error(e)
    }
}